package com.example.codingchallenge;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.att.intern.webservice.*;

public class MainActivity extends Activity {

	private EditText userET;
	private EditText passET;
	private EditText verifyET;
	
	private Button loginButton;
	
	private String userName;
	private String passName;
	
	// Strings that are returned from login().
	private String token;
	private String securityLvl;
	private String status;
	
	Webservice webServ;
	
	public static boolean logoff;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		userET = (EditText) findViewById(R.id.userIdTextField);
		passET = (EditText) findViewById(R.id.passwordTextField);
		verifyET = (EditText) findViewById(R.id.verifyTextField);
		
		loginButton = (Button) findViewById(R.id.loginButton);
		
		logoff = false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public static void hideSoftKeyboard(Activity activity) {
	    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
	    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		userET.setText("");
		passET.setText("");
		verifyET.setText("");
		
		userET.setFocusableInTouchMode(true);
		passET.setFocusableInTouchMode(true);
		loginButton.setEnabled(true);
		
		userET.requestFocus();
	}
	
	public void loginClick(View view)
	{
		verifyET.setText("Loading...");
	
		// Load progress bar
        ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true);
        dialog.setCancelable(true);
        
		// AsyncTask thread
		final ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar);
		new TestLogin(this, progress).execute();
		
	}
	
	
	final class TestLogin extends AsyncTask<Void, Integer, String[]>
	{
		private final Activity parent;
		private final ProgressBar progress;
		
		public TestLogin(final Activity parent, final ProgressBar progress)
		{
			this.parent = parent;
			this.progress = progress;
		}
		
		@Override
		protected void onPreExecute()
		{
			// EASIER TESTING PURPOSES
			userET.setText("admin");
			passET.setText("password");
			// ////////////////////////
			
			userET.setFocusable(false);
			passET.setFocusable(false);
			loginButton.setEnabled(false);
			
			progress.setVisibility(0);
			hideSoftKeyboard(parent);
			
			
		}

		@Override
		protected String[] doInBackground(Void... arg0) 
		{			
			// Receive the userID and password from login.
			userName = userET.getText().toString();
			passName = passET.getText().toString();
			
			// DEBUG purposes
			System.out.println("UserID: " + userName);
			System.out.println("Password: " + passName);
			
			// Verify login credentials.
			Webservice webServ = new Webservice(MainActivity.this); 
			
			// Update verify text field.
			//verifyET.setText(R.string.verify_login);
			
			
			// Test login.
			return webServ.login(userName, passName);
		}
		
		
		@Override
		protected void onPostExecute(String[] pumpkin) {
			super.onPostExecute(pumpkin);
			guiControl(pumpkin);

		}

		protected void guiControl(String[] pumpkin)
		{
			if (pumpkin[0] == null)
			{
				verifyET.setText("Access DENIED!");	
			}
			else
			{ 
				verifyET.setText("ACCESS GRANTED!");
				
				token = pumpkin[0]; System.out.println("token-1: " + token);
				securityLvl = pumpkin[1];
				status = pumpkin[2];
				
				System.out.println("token: " + token);

				java.util.Date date = new java.util.Date();
				String timeStamp = new Timestamp(date.getTime()) + "";
				//System.out.println(timeStamp);
				
				Intent intent = new Intent(MainActivity.this, SearchLocations.class);
				intent.putExtra("timeStamp", timeStamp);
				intent.putExtra("token", token);
				intent.putExtra("securityLvl", securityLvl);
				intent.putExtra("status", status);

				startActivity(intent);
			}
		}
		
	}
	
}
